package checkTimeConnect 
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.NetConnection;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.text.TextField;
	import recordVod.events.EventData;
	import flash.net.Responder;	
	import flash.utils.setTimeout;
	import flash.external.ExternalInterface;
	/**
	 * ...
	 * @author Bamboo
	 */
	public class CheckFMS  extends MovieClip
	{
		
		public var _connectGame:NetConnection;
		private var _clientObject:Object;
			
		private var _fmsPath:String;
		private var _roomId:String;
		private var _dateStart:uint;
		private var _dateEnd:uint;
		private var _dateConnectStart:String;
		private var _dateConnectEnd:String;
		private var _totalTime:uint;
		private var time:uint = 0;
		public var txt:TextField;
		public var totalTime:TextField;
		public var txtStatus:TextField;
		public var rtmpTxt:TextField;
		public var connectBtn:MovieClip;
		private var hasFinished:Boolean = false;
		private var counter:int = 0;
		public function CheckFMS() 
		{
			connectBtn.addEventListener(MouseEvent.CLICK, hdlConnectFms);
			connectBtn.buttonMode = true; 
			ExternalInterface.addCallback("getConnect", getConnect);
		}
		
		public function getConnect(rtmp:String) {
			hasFinished = false;
			_fmsPath = rtmp;
			_roomId = "1";
			txt.text = "";
			totalTime.text = "";
			txtStatus.text = "";
			time = 0;
			_totalTime = 0;
			counter = 0;
			connectFMS();
		}
		
		private function hdlConnectFms(e:Event):void 
		{
			_fmsPath = rtmpTxt.text;// "113.21.208.2/boo";//"localhost/vod";//rtmpTxt.text;//
			_roomId = "1";
			txt.text = "";
			totalTime.text = "";
			txtStatus.text = "";
			time = 0;
			_totalTime = 0;
			connectFMS();
			trace("_fmsPath:" + _fmsPath);
		}
		
		
		
		
		private function updateTxt(value:String, textFl:TextField):void {
			var temp:String = textFl.text;
			textFl.text =  value +"\n" + temp;
			
		}
		
		
		
		//do connect FMS
		public function connectFMS():void {
			
			counter++;
			trace("Create new conneciton." + counter.toString());
			_connectGame = new NetConnection();
			_clientObject = new Object();
			
			var mydate:Date = new Date();
			_dateStart = mydate.getMinutes() * 60 * 1000 + mydate.getSeconds() * 1000 + mydate.getMilliseconds();
			
			_dateConnectStart = mydate.getHours() + ":" + mydate.getMinutes() +":" + mydate.getSeconds() + ":" + mydate.getMilliseconds();
		
			_connectGame.connect(("rtmp://" + _fmsPath + "/" + _roomId));
			ExternalInterface.call("startCall");
			//trace("rtmp://" + _fmsPath+ "/" + _roomId);
			_connectGame.client = _clientObject;
			_connectGame.addEventListener(NetStatusEvent.NET_STATUS, netStatusNcHdl);
			_connectGame.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHdl);
			//_clientObject["onBWCheck"] = onBWCheck
			//_clientObject["onBWDone"] = onBWDone;
		}
		
		public function onBWCheck(... rest):Number {  
			return 0;  
		}  
		public function onBWDone(... rest):void {  
			var p_bw:Number;  
			if (rest.length > 0) p_bw = rest[0];  
			// your application should do something here  
			// when the bandwidth check is complete  
			//trace("bandwidth = " + p_bw + " Kbps."); 
			updateTxt("Bandwidth = " + p_bw + " Kbps.", txt);
		 }   
		
		private function netStatusNcHdl(e:NetStatusEvent):void {
			
			updateTxt("Start connect       :"+_dateConnectStart, txt);
			var mydate:Date = new Date();
			_dateEnd = mydate.getMinutes() * 60 * 1000 + mydate.getSeconds() * 1000 + mydate.getMilliseconds();
			_dateConnectEnd = mydate.getHours() + ":" + mydate.getMinutes() +":" + mydate.getSeconds() + ":" + mydate.getMilliseconds();
			trace("netStatusNcIdHdl:" + e.info.code);
			var strTemp:String;
			strTemp = totalTime.text;
			
			switch(e.info.code) {
				case "NetConnection.Connect.Success":
					updateTxt("Connect.Success :" + _dateConnectEnd, txtStatus);
					//Bussiness for time
					time = time + 1;
					_totalTime = _totalTime + _dateEnd - _dateStart;
					trace()
					var average:uint = _totalTime / time;
					var temp:String = (_dateEnd - _dateStart).toString();
					
					updateTxt("Time: " + time + "        Time take: "+ temp +  "(ms) 	Average: " + average + "(ms)", totalTime);
					//_connectGame.call("checkBandwidth", null);
					ExternalInterface.call("resultOut", (_dateEnd - _dateStart), e.info.code);
					break;
				case "NetConnection.Connect.Failed":
					updateTxt("Connect.Failed :" + _dateConnectEnd, txtStatus);
					totalTime.text = "\n" + strTemp;
					if (hasFinished == false)
					{
						hasFinished = true;
						if(_connectGame != null)
							_connectGame.close();
						
					}
					connectFMS();
					ExternalInterface.call("resultOut", -1 , e.info.code);
					break;
				case "NetConnection.Connect.Rejected":
					updateTxt("Connect.Rejected :" + _dateConnectEnd, txtStatus);
					totalTime.text = "\n" + strTemp;
					if (hasFinished == false)
					{
						hasFinished = true;
						if(_connectGame != null)
							_connectGame.close();
						
					}
					ExternalInterface.call("resultOut", -1 , e.info.code);
					break;
				case "NetConnection.Connect.Closed":
					updateTxt("Connect.Closed :" + _dateConnectEnd, txtStatus);
					totalTime.text = "\n" + strTemp;
					if (hasFinished == false)
					{
						hasFinished = true;
						if(_connectGame != null)
							_connectGame.close();
						
					}
					connectFMS();
					ExternalInterface.call("resultOut", -1 , e.info.code);
					break;
					
			}
			
		
		}	
		
		
		
		/**
		 * @author Bamboo
		 */
		private function securityErrorHdl(e:SecurityErrorEvent):void {
			trace("SecurityErrorHdl: " + e);
		}	
		
			
		private function failCallFMS(status:Object):void {
			for (var p:String in status){
				trace("FAIL CALL FMS:  " + p + ": " + status[p]);
			} 
		}
		
		private function successCallFMS(status:Object):void { 
			
		}
		
	}

}